package com.wj.opratorlog.utils;/**
 * @author
 * @description
 * @create 2024-07-08-14:40
 **/

import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;

import java.util.Locale;

/**
 * @author cnry
 * @date 2024/7/8
 * @description
 **/


public class MessageUtils {


    public static MessageSource messageSource;

    public MessageUtils(MessageSource messageSource) {
        // spring的bean注入
        MessageUtils.messageSource = messageSource;
    }

    /**
     * 获取国际化翻译值
     */
    public static String get(String msgKey) {
        return messageSource.getMessage(msgKey, null, LocaleContextHolder.getLocale());
    }

    /**
     * 获取中文翻译值
     */
    public static String getWithChina(String msgKey) {
        return messageSource.getMessage(msgKey, null, Locale.CHINA);
    }

    /**
     * 获取国际化翻译值（包括占位符）
     */
    public static String get(String msgKey, Object[] objects) {
        return messageSource.getMessage(msgKey, objects, LocaleContextHolder.getLocale());
    }


}
